/*
 * Copyright (c) 2010-2011 Plugger Antonio Begue Ponce. All Rights Reserved.
 *
 * This file is part of Plugger Framework.
 *
 *  Plugger Framework is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License version 3 as published by
 *  the Free Software Foundation.
 *
 *  Plugger Framework is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Plugger Framework.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.plugger.repository.impl.hibernate;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.plugger.infrastructure.domainbase.IAggregateRoot;
import org.plugger.infrastructure.repositorybase.IWorkspace;
import org.plugger.infrastructure.repositorybase.RepositoryBase;

/**
 * A abstract hibernate repository implementation.
 * 
 * @author Antonio Begue
 * @version $Revision: 1.0 $
 */
public abstract class HRepositoryBase<T extends IAggregateRoot> extends RepositoryBase<T> {
    /**
     * A HRepositoryBase constructor that receive a workspace.
     * @param ws A workspace object.
     */
    protected HRepositoryBase(IWorkspace ws) {
        super(ws);
    }

    /**
     * Return A new Criteria from the hibernate workspace.
     * @return An org.hibernate.Criteria object.
     */
    protected Criteria createCriteria() {
        HTransactionWorkspace ws = (HTransactionWorkspace) super.getWorkspace();
        return ws.getSession().createCriteria(this.getPersistentClass());
    }

    /**
     * Return A new Query from the hibernate workspace.
     * @return An org.hibernate.Query object.
     */
    protected Query createQuery(String query) {
        HTransactionWorkspace ws = (HTransactionWorkspace) super.getWorkspace();
        return ws.getSession().createQuery(query);
    }

    /**
     * Return A new NamedQuery from the hibernate workspace.
     * @return An org.hibernate.Query object.
     */
    protected Query createNamedQuery(String name) {
        HTransactionWorkspace ws = (HTransactionWorkspace) super.getWorkspace();
        return ws.getSession().getNamedQuery(name);
    }
}
